package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.model.AdminUserModel;
import com.app.model.CategoryModel;
import com.app.model.DepartmentModel;
import com.app.service.AdminUserService;
import com.app.service.CategoryService;
import com.app.service.DepartmentService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CategoryVo;
import com.app.vo.DepartmentVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Controller
@RequestMapping("/api")
public class CategoryController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private CategoryService categoryService;


    /**
     * 公用数据列表
     *
     * @param page
     * @param limit
     * @param name
     * @param request
     * @return
     */
    @GetMapping("/getCategoryList")
    public Result<Object> getDepList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam(required = false) String name,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        Page<CategoryModel> modelPage = categoryService.categoryPageList(page, limit, name);
        int count = (int) modelPage.getTotal();
        List<CategoryModel> list = categoryService.getCatByP(0);
        List<CategoryModel> depModelList = modelPage.getRecords();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setChild(depModelList);
        }
        CategoryVo vo = new CategoryVo();
        vo.setCount(count);
        vo.setList(list);
        return Result.success(vo);
    }

    /**
     * 公用数据编辑
     *
     * @param model
     * @param request
     * @return
     */
    @PostMapping("/editCategory")
    public Result<Object> getDepList(
            @RequestBody CategoryModel model,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (model.getId() != null && model.getId() > 0) {
            model.setUpTime(CommonHelp.getNowTimestamp());
            categoryService.update(model);
        } else {
            model.setCrtTime(CommonHelp.getNowTimestamp());
            categoryService.insert(model);
        }
        return Result.success("操作成功");
    }

    @GetMapping("/categoryStatus")
    public Result<Object> categoryStatus(
            @RequestParam("id") Integer id,
            @RequestParam("status") Integer status,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        CategoryModel model =  categoryService.getCategoryById(id);
        model.setStatus(status);
        categoryService.update(model);
        return Result.success("操作成功");
    }

    /**
     * 查询全部数据树状结构
     *
     * @param request
     * @return
     */
    @GetMapping("/getCategoryAllList")
    public Result<Object> getDepAllList(
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        List<CategoryModel> list = findChildren(categoryService.getAll(), 0);
        return Result.success(list);
    }

    /**
     * 查询问题列表
     *
     * @param request
     * @return
     */
    @GetMapping("/getCategoryListByPid")
    public Result<Object> getCategoryListByPid(
            HttpServletRequest request) {
        List<CategoryModel> list = categoryService.getListByPid();
        return Result.success(list);
    }

    private List<CategoryModel> findChildren(List<CategoryModel> modelList, Integer pid) {
        List<CategoryModel> personModelList = new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            if (modelList.get(i).getPid().equals(pid)) {
                CategoryModel model = modelList.get(i);
                List<CategoryModel> modelList1 = findChildren(modelList, model.getPid());
                for (int j = 0; j < modelList1.size(); j++) {
                    if (modelList1 != null && modelList1.size() > 0) {
                        model.setChild(modelList1);
                    }
                }
                personModelList.add(model);
            }
        }
        return personModelList;
    }

}
